import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class DriverUtil {
    public static WebDriver getDriver() {
        //使⽤插件管理⼯具webdrivermanager
        WebDriverManager.chromedriver().setup();
//添加浏览器配置
        ChromeOptions options = new ChromeOptions();
//1)允许任何来源的远程连接
        options.addArguments("--remote-allow-origins=*");
//创建浏览器驱动对象
        ChromeDriver driver = new ChromeDriver(options);
//访问百度⽹⻚，搜索“在线抽奖系统”
        driver.get("http://59.110.23.211:9090/blogin.html");
        return driver;
    }

    public static String isPopupAppear(WebDriver driver) {
        boolean isPopupAppear = false;
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
        String text=null;
        try {
            // 等待弹窗出现（若3秒内出现，则会捕获到Alert对象）
            Alert alert = wait.until(ExpectedConditions.alertIsPresent());
            isPopupAppear = true; // 弹窗出现
            text = alert.getText(); // 获取弹窗文本
            alert.accept(); // 可选：处理弹窗（如点击确定）
        } catch (Exception e) {
            // 超时未出现弹窗 → 不会出现
            isPopupAppear = false;
        }
        if(isPopupAppear){
            return text;
        }else{
            return "";
        }
    }

}
